<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Homemenu;
use app\admin\model\Setting;
use think\Request;

class  Homemenus extends Base
{
    //列表
    public function lists(){
        $user = $this->user_info();
        $Homemenu = Homemenu::where("pid",0)->order(["sort"=>"asc"])->select();
        $newHomemenu = [];
        foreach($Homemenu as $val){
          array_push($newHomemenu,$val);
          $twoHomemenu = Homemenu::where("pid",$val['id'])->order("sort","asc")->select();
          foreach($twoHomemenu as $v){
            array_push($newHomemenu,$v);
          }
        }
        foreach($newHomemenu as $k => $val){
            if($val['pid'] == 0){
              $newHomemenu[$k]['pname'] = "顶级栏目";
            }else {
                $res = Homemenu::where(array("id" => $val['pid']))->find();
              $newHomemenu[$k]['pname'] = $res['title'];
            }
        }
        //修改所属栏目
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" =>$newHomemenu,
        );
        return view("list",$data);
    }

    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>Homemenu::where(array("pid"=>"0"))->order("sort asc")->select(),
        );
        return view("addform",$data);
    }
    public function add(Request $request){
        $data = $request->post();
        if(Homemenu::insert($data)){
            $this->success("添加成功","Homemenus/lists");
        }else{
            $this->error("添加失败","Homemenus/addform");
        }

    }
    //编辑
    public function update(Request $request){
        $user = $this->user_info();
        $Homemenu = Homemenu::where("pid","0")->order("sort asc")->select();
        foreach($Homemenu as $k => $val){
            if($val['pid'] == 0){
                $Homemenu[$k]['pname'] = "顶级栏目";
            }else {
                $res = Homemenu::where(array("id" => $val['pid']))->find();
                $Homemenu[$k]['pname'] = $res['title'];
            }
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Homemenu::where(array('id'=>$request->get('id')))->find(),
            "list" => $Homemenu,
        );
        return view("update",$data);
    }
    public function edit(Request $request){
        $data = $request->post();
        if(Homemenu::update($data)){
            $this->success("修改成功","Homemenus/lists");
        }else{
            $this->error("修改失败","Homemenus/update");
        }

    }
    //删除
    public function delete(Request $request){
        $user = $this->user_info();
        if(Homemenu::destroy($request->post('id'))){
            $this->success("删除成功","Homemenus/lists");
        }else{
            $this->error("删除失败","Homemenus/lists");
        }
    }

    //所有图标页面
    public function fontawesome(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("fontawesome",$data);
    }

    //修改状态
    public function setStatus(Request $request){
        $data = $request->post();
        if(Homemenu::update($data)){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
    //上传文件
    public function upload(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');
        if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'navigatorimage';

            // 移动到框架应用根目录/public/uploads/ 目录下

            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                dump($error);exit;
            }
            if($info){
                // 成功上传后 获取上传信息

                //获取图片的名字
                $imgName = $info->getFilename();
                //获取图片的路径
                $photo= $imgName;

            }else{
                // 上传失败获取错误信息
                $file->getError();
            }
        }else{
            $photo = '';
        }
        if($photo !== ''){
            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }
    }

}
